import React from 'react'
import SetButton from '../SetBtton'
import {inject, observer} from 'mobx-react'
import {message} from 'antd'
import {withRouter} from 'react-router-dom'

const HOCSetDeliveryArea = WarpComponent => {
    @withRouter
    @inject('setStoreInfo')
    @observer
    class ElementComponent extends React.Component {
        state = {
            data: [],
            columns: [
                {
                    title: "ID",
                    dataIndex: "key",
                    align:'center'
                },
                {
                    title: "配送区域名称",
                    dataIndex: "dasNoda",
                    align:'center'
                },
                {
                    title: "所辖区域",
                    dataIndex: "value",
                    align:'center'
                },
                {
                    title: "操作",
                    align:'center',
                    render: (text,record) => (
                        <div>
                            <SetButton txt="修改" onClick={()=>this.modifyBtn(record)}></SetButton>
                            <SetButton txt="删除" onClick={()=>this.delStoreList(record)}></SetButton>
                        </div>
                    ),
                },
            ],
            pagination:{
                defaultCurrent:1,
                defaultPageSizes:5,
                hideOnSinglePage:true
            },
        }
        componentDidMount(){
            this.getInitData()
        }
        //初始化
        getInitData=()=>{
            let {getDistributeArea} = this.props.setStoreInfo
            getDistributeArea().then(data=>{
                if (data.data.code === 200) {
                    let allData = data.data.data
                    allData.forEach(element => {
                        element.key = element.dasId
                        element.value = element.ctdaList.map(item=> item.countyName)
                        element.value = element.value.join(', ')
                    });
                    let obj = this.state
                    obj.data = allData
                    this.setState(obj)
                }else{
                    message.error('查询数据失败')
                }
            })
        }
        //添加
        addArea= async ()=>{
            let {clearSetStore} = this.props.setStoreInfo
            await clearSetStore()
            await this.props.history.push('/Home/SheZhi/SetDeliveryArea/AddDeliveryArea')
        }
        //修改
        modifyBtn= async (record)=>{
            let {getCity, getProvince, getSetProvence, getSetCities, getArea} = this.props.setStoreInfo
            let {ctdaList, dasId, dasNoda, dasCcm, dasFc, dasRc, dasFq, dasIk, dasState} = record //获取区县
            let countyId = ctdaList[0].countyId //获取区县ID
            //获取城市ID
            let cityId = await getCity(countyId).then(data=>{
                if (data.data.code === 200) {
                    return data.data.data
                }else{
                    message.error('查询城市失败')
                }
            })
            // 获取省份ID
            let provinceId = await getProvince(cityId).then(data=>{
                if (data.data.code === 200) {
                    return data.data.data
                }else{
                    message.error('查询省份失败')
                }
            })
            //获取所有省份
            let provinceData = await getSetProvence().then(data=>{
                if (data.data.code === 200) {
                    return data.data.data
                }else{
                    message.error('查询所有省份信息失败')
                }
            })
            let provinceName = provinceData.filter(item=>item.provinceId === provinceId)[0].provinceName
            //获取当前省对应的市
            let cityData = await getSetCities(provinceId).then(data=>{
                if (data.data.code === 200) {
                    return data.data.data
                }else{
                    message.error('请求城市信息失败')
                }
            })
            let cityName = cityData.filter(item=>item.cityId === cityId)[0].cityName
            //获取当前省当前市对应的所有区
            let countyData = await getArea(cityId).then(data=>{
                if (data.data.code === 200) {
                    return data.data.data
                }else{
                    message.error('查询区县失败')
                }
            })
            let storeArea = ['中国', provinceName, cityName]
            let das = []
            ctdaList.forEach(element => {
                das.push(element.countyId)
            });
            let tranceData = {
                dasId, //区域ID
                dasNoda, //区域名称
                dasCcm, //费用计算方式
                dasFc, //首重费用
                dasRc, //续重费用
                dasFq, //免费额度
                dasIk,
                dasState,
                das,
                storeArea,
                countyData
            }

            let {saveSetStore} = this.props.setStoreInfo
            await saveSetStore(tranceData)
            await this.props.history.push('/Home/SheZhi/SetDeliveryArea/AddDeliveryArea')
        }
        //删除
        delStoreList=(record)=>{
            let {dasId} = record
            let {delSingle} = this.props.setStoreInfo
            delSingle(dasId).then(data=>{
                if (data.data.code === 200) {
                    message.success('删除成功')
                    this.getInitData()
                }else{
                    message.error('删除失败')
                }
            })
        }














        render() {
            let methods = {
                addArea:this.addArea
            }
            return (
                <div>
                    <WarpComponent {...this.state} {...methods}></WarpComponent>
                </div>
            );
        }
    }
    return ElementComponent
}

export default HOCSetDeliveryArea